package uni.UNIBEE7B8D.view.User.LoginUserPwd;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.ButterKnife;
import uni.UNIBEE7B8D.BaseActivity;
import uni.UNIBEE7B8D.Presenter.LoginUserPersenter;
import uni.UNIBEE7B8D.R;

public class LoginUserPwdActivity extends BaseActivity implements View.OnClickListener, LoginUserPwdContract.LoginUserPwdView {

    @BindView(R.id.guanbi)
    ImageView guanbi;
    @BindView(R.id.loginText)
    TextView loginText;
    @BindView(R.id.loginText1)
    TextView loginText1;
    @BindView(R.id.userName)
    EditText userName;
    @BindView(R.id.userPwd)
    EditText userPwd;
    @BindView(R.id.pwdView)
    LinearLayout pwdView;
    @BindView(R.id.mima_tishi)
    LinearLayout mimaTishi;
    @BindView(R.id.login)
    Button login;
    @BindView(R.id.forget)
    TextView forget;
    @BindView(R.id.zhuce)
    TextView zhuce;


    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private LoginUserPersenter loginUserPersenter = new LoginUserPersenter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_user_pwd);
        ButterKnife.bind(this);

        sharedPreferences = getSharedPreferences("kangconfig", Activity.MODE_PRIVATE);
        editor = sharedPreferences.edit();

        zhuce.setOnClickListener(this);
        login.setOnClickListener(this);
        guanbi.setOnClickListener(this);
        forget.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.forget:
                Intent intent = new Intent(getBaseContext(), ForgetPwdActivity.class);
                startActivity(intent);
                break;
            case R.id.guanbi:
                finish();
                break;
            case R.id.login:
                loginUserPersenter.login(userName.getText().toString().trim(), userPwd.getText().toString().trim());
                break;
            case R.id.zhuce:
                finish();
                break;
        }
    }

    @Override
    public void Toast(String s) {
        Toast.makeText(getBaseContext(), s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void login(Object object) {
        Log.d("==",new Gson().toJson(object));
        editor.putString("userMessage", new Gson().toJson(object));
        editor.putString("access_token", "zhangsan");
        editor.commit();
        finish();
    }
}
